<?php
class Admin_ProductController extends Zend_Controller_Action
{
	private $model;
	private $auth;
	private $acl;
	public function init()
	{
		$this->model = new Admin_Model_Product();
		$this->auth = Zend_Auth::getInstance();
		$this->acl = new Admin_Model_Access();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/admin";
		$dirlayout = APPLICATION_PATH . "/layouts/admin";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/style.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/upload/uploadify.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-ui-1.8.18.custom.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slidedown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/datepick2.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/jquery.uploadify-3.1.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/jquery.uploadify-3.1.min.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/upyoutb.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/uploadimg.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/uploadvideo.js');
		$this->view->headScript()->appendFile($dirfile . '/js/nicEdit.js');
		$this->view->headScript()->appendFile($dirfile . '/js/editor1.js');
		$this->view->headScript()->appendFile($dirfile . '/js/datepick1.js');
		$this->view->headScript()->appendFile($dirfile . '/js/updateselect.js');
		$this->view->headScript()->appendFile($dirfile . '/js/addproduct.js');
		$this->view->headScript()->appendFile($dirfile . '/js/newproduct.js');
		$this->view->headScript()->appendFile($dirfile . '/js/scpro.js');
		$this->view->headScript()->appendFile($dirfile . '/js/scepro.js');
		$this->view->headScript()->appendFile($dirfile . '/js/editpro.js');
		$this->view->headScript()->appendFile($dirfile . '/js/getCategory.js');
		$this->view->headScript()->appendFile($dirfile . '/js/getProduct.js');	
		$this->view->headScript()->appendFile($dirfile . '/js/delpro.js');
		//$this->view->headScript()->appendFile($dirfile . '/upload/upyoutube.js');
		//$this->view->headScript()->appendFile($dirfile . '/upload/upyoutb.js');
		$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		Zend_Layout::startMvc($option);
	}
	public function indexAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','index'))
		{
		$data = $this->model->getallnamecatelog();
		$this->view->option = $data;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function addAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','add'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		$name = $this->_request->getParam('name');
		$catelog = $this->_request->getParam('catelog');
		$updatedate = $this->_request->getParam('updatedate');
		$count = $this->_request->getParam('count');
		$price = $this->_request->getParam('price');
		$descrip = $this->_request->getParam('textarea');
		$idcatelog = $this->model->getidcatelog($catelog);
		$id_user = $this->auth->getIdentity()->id_user;
		$dirpath = APPLICATION_PATH . '/../public/';
		$name_folder = 'product'. $id;
		$dir_folder = $dirpath.$idcatelog['folder_category'].'/'.$name_folder;
		if(!is_dir($dir_folder))
		{
			mkdir($dir_folder,0775,true);
		}
		  try{
		$insert = $this->model->add($id,$name, $idcatelog['id_catelog'], $updatedate, $count, $price, $descrip,$id_user,$name_folder);
		echo "Add thanh cong";
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}  
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function editAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','edit'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		$name = $this->_request->getParam('name');
		$catelog = $this->_request->getParam('catelog');
		$updatedate = $this->_request->getParam('updatedate');
		$count = $this->_request->getParam('count');
		$price = $this->_request->getParam('price');
		$descrip = $this->_request->getParam('textarea');
		$idcatelog = $this->model->getidcatelog($catelog);
		$id_user = $this->auth->getIdentity()->id_user;
		try {
			$update = $this->model->edit($id, $name, $idcatelog['id_catelog'], $updatedate, $count, $price,$descrip, $id_user);
			echo "Edit thanh cong";
		}catch (Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function getcategoryAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','getcategory'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$select = $this->_request->getParam('select');
		$id_category = $this->model->getidcatelog($select);
		$data = $this->model->getproduct($id_category['id_catelog']);
		echo Zend_Json::encode($data);
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function getproductAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','getproduct'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$select = $this->_request->getParam('select');
		$id_product = $this->model->getidproduct($select);
		$data = $this->model->getvideo($id_product);
		echo Zend_Json::encode($data);
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function uploadimgAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','uploadimg'))
		{
		$data_product = $this->model->getallproduct();
		$this->view->product = $data_product;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
		
	}
	public function uploadvideoAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','uploadvideo'))
		{
		$data_catelog = $this->model->getallnamecatelog();
		$this->view->category = $data_catelog;
		$this->view->id_user = $this->auth->getIdentity()->id_user;
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function uploadfileimgAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','uploadfileimg'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		/* $product = $this->_request->getParam('product');
		$video = $this->_request->getParam('video');
		$id_product = $this->model->getidproduct($product); */
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
			try {
				$upload->receive();
				if ($upload->isUploaded())
				{
					/* foreach ($fileinfo as $file=>$tempinfo)
					{
					$name_url = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
					$insert= $this->model->addimg($name_url, $tempinfo['name'], $tempinfo['size'],$id_product['id_product']);
					} */
					$img = new Zend_Session_Namespace('image');
					$img->fileinfo = $fileinfo;
				//	echo Zend_Debug::dump($fileinfo);
				}
			} catch (Exception $e) {
				echo $e->getMessage();
			}
			//echo Zend_Debug::dump($upload->getFileInfo());
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function upyoutubeAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','upyoutube'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$category = $this->_request->getParam('category');
		$product = $this->_request->getParam('product');
		$path = $this->_request->getParam('path');
		$id_product = $this->model->getidproduct($product);
		$name_video = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$user = $this->_request->getParam('id_user');
		//$user = $this->auth->getIdentity()->id_user;
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
		try {
			$upload->receive();
			if ($upload->isUploaded())
			{
				foreach ($fileinfo as $file=>$tempinfo)
				{
				$name_img = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
				$insert= $this->model->addvideoytb($name_video, $date, $path,$tempinfo['name'],$name_img,$tempinfo['size'], $id_product['id_product'], $user, 1);
			echo "Upload Success";
				}
			}
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function uploadfilevideoAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','uploadfilevideo'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$category = $this->_request->getParam('category');
		$product = $this->_request->getParam('product');
		//$path = $this->_request->getParam('path');
		$id_product = $this->model->getidproduct($product);
		$name_video = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$id_user = $this->_request->getParam('id_user');
		$img = new Zend_Session_Namespace('image');
		$fileimg = $img->fileinfo;
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
			try {
				$upload->receive();
				if ($upload->isUploaded())
				{
						foreach ($fileinfo as $file=>$tempinfo)
						{
						$name_url = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
						$name_img = $this->_request->getBaseUrl() . '/public/' . $fileimg['file']['name'];
						$insert= $this->model->addvideo($name_video, $date, $name_url, $tempinfo['size'],$fileimg['file']['name'],$name_img,$fileimg['file']['size'], $id_product['id_product'], $id_user, 2);
						}
				}
			} catch (Exception $e) {
				echo $e->getMessage();
			}
	
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function getsearchAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','getsearch'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$contents = $this->_request->getParam('text-search');
		$session = new Zend_Session_Namespace('searchproduct');
		$session->content = $contents;
		$this->_forward('search');
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function manageAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','manage'))
		{
		$data = $this->model->getAll();
		$paging = Zend_Paginator::factory($data);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->paging = $paging;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function searchAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','search'))
		{
		$session = new Zend_Session_Namespace('searchproduct');
		$contents = $session->content;
		$search = $this->model->search($contents);
		$pagingsearch = Zend_Paginator::factory($search);
		$pagingsearch->setItemCountPerPage(2);
		$pagingsearch->setPageRange(3);
		$currentsearch = $this->_request->getParam('page',1);
		$pagingsearch->setCurrentPageNumber($currentsearch);
		$this->view->pagingsearch = $pagingsearch;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function vieweditAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','viewedit'))
		{
		$id = $this->_request->getParam('id');
		$data = $this->model->getId($id);
		$datacate = $this->model->getallnamecatelog();
		$this->view->data = $data;
		$this->view->option = $datacate;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function deleteAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'product','delete'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		try {
			$delete = $this->model->delete($id);
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
}